import formatter from '@/common/formatter';
function getChartData([dataPool1, dataPool2, topStaff, kuCun], selectedDepartment = []) {
  selectedDepartment = selectedDepartment.flat().map(item => item === '全市' ? '市仓' : item);
  const KC_NAME = 'guangmao';
  const zaiTu = dataPool1.kuandai_zt.all;

  const matchkuCun = selectedDepartment.length ? kuCun.filter(item => selectedDepartment.includes(item.department)) : kuCun.filter(item => item.department !== '其它');
  const kuCunShu = matchkuCun.reduce((acc, cur) => acc + (cur[KC_NAME] || 0), 0)

  const maxDate = Math.max(...zaiTu.map(item => item.date)),
    zaiTuShu = zaiTu.filter(item => (item.date === maxDate) && (!selectedDepartment.length || (selectedDepartment.includes(item.region) || selectedDepartment.includes(item.grid)))).reduce((acc, cur) => acc + cur.count, 0);
  this.series[0].data[0].value = zaiTuShu;
  this.series[0].max = kuCunShu;
  this.series[0].detail.formatter = '{value}/' + kuCunShu;

  const progress = zaiTuShu / kuCunShu;
  let color = '#3E9B1C';
  if (progress > 0.9) {
    color = '#DB3824';
  } else if (progress > 0.7) {
    color = '#DBB523';
  } else if (progress > 0.5) {
    color = '#27B4C2';
  }
  this.series[0].progress.itemStyle.color = color;

  this.tooltip.formatter = (params) => {
    const marker = `<span style=\"display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px;background-color:${color};\"></span>`;
    let tip = `<div> ${marker} ${params.seriesName} ${formatter.valueToText(progress, true)}</br>`;
    tip += `在途${zaiTuShu} / 库存${kuCunShu}`;
    return tip;
  }

}

export default {
  title: {
    text: '宽带在途/光猫库存',
    left: 'center'
  },
  tooltip: {},
  series: [
    {
      name: '在途占比',
      type: 'gauge',
      max: 0,
      axisLabel: {
        formatter: (value) => value.toFixed(0)
      },
      progress: {
        show: true,
        itemStyle: {}
      },
      detail: {
        valueAnimation: true,
        formatter: '{value}/' + 0,
        fontSize: 16,
        offsetCenter: [0, '75%']
      },
      data: [{ value: 0 }]
    }
  ],
  getChartData
};
